package org.zxl.springbootdemo.exception.advices;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

@Aspect
@Component
public class ParamCheckAspect {
    private final Log log = LogFactory.getLog(ParamCheckAspect.class);

    //拦截指定包名下的所有类的所有方法
    @Pointcut("execution(* com.unionpay.ufp.juhe.service..*.*(..))")
    public void paramCheckPointCut(){}

    @Before("paramCheckPointCut()")
    public void before(JoinPoint joinPoint ) throws Exception{
//        joinPoint.getSignature();
//        String methodName = joinPoint.getSignature().getName();
        try {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Object[] objects = joinPoint.getArgs();
            Annotation[][] parameterAnnotations= methodSignature.getMethod().getParameterAnnotations();

            for (int i = 0; i <objects.length; i++) {
                Annotation[] annotations = parameterAnnotations[i];
                if(annotations!=null){
                    for(Annotation annotation: annotations){
                        if(annotation instanceof Valid){
                            //获取
                            Class clazz = objects[i].getClass();
                            Field[] fields1 = clazz.getFields();
                            Field[] fields2 = clazz.getDeclaredFields();
                            Field[] fields = ArrayUtils.addAll(fields1, fields2);
                            for(Field field : fields){
                                NotNull notNull = field.getAnnotation(NotNull.class);
                                if(notNull != null){
                                    field.setAccessible(true);
                                    Object fieVal = field.get(objects[i]);
                                    if(fieVal == null){
                                        throw new Exception(notNull.message());
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
//                FieldSignature fieldSignature = (FieldSignature) objects[i];
//                Valid valid = fieldSignature.getField().getAnnotation(Valid.class);
//                if(valid != null){
//
//                }

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

}
